package com.hyl.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * LoginInterceptor
 *
 * @author hyl
 * @date 2025-07-02
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       //获取请求的URL
        String requestURI = request.getRequestURI();
        //判断请求是否为登录页面，如果是则放行
        if (requestURI.contains("login") ||
                requestURI.contains("code") ||
                requestURI.contains("/phone") )
        {
            return true;
        }

        //验证用户是否登录
        HttpSession session = request.getSession();
        if (session.getAttribute("loginUser") == null) {
            response.sendRedirect("/login");
        }
        return true;
    }
}
